package xyz.linyuxb.utils;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobKey;
import org.quartz.utils.Key;

/**
 * @Author: linyuxb
 * @Date: 2021/11/14 11:44 上午
 * @Desc: 自定义job构建器
 */
public class MyJobBuilder {

    private JobKey key;
    private String description;
    private Class<? extends Job> jobClass;
    private boolean durability;
    private boolean shouldRecover;

    private JobDataMap jobDataMap = new JobDataMap();

    protected MyJobBuilder() {
    }

    public static MyJobBuilder newJob() {
        return new MyJobBuilder();
    }

    public static MyJobBuilder newJob(Class <? extends Job> jobClass) {
        MyJobBuilder b = new MyJobBuilder();
        b.ofType(jobClass);
        return b;
    }

    public MyJobDetailImpl build() {

        MyJobDetailImpl job = new MyJobDetailImpl();

        job.setJobClass(jobClass);
        job.setDescription(description);
        if(key == null){
            key = new JobKey(Key.createUniqueName(null), null);
        }
        job.setKey(key);
        job.setDurability(durability);
        job.setRequestsRecovery(shouldRecover);

        if(!jobDataMap.isEmpty()){
            job.setJobDataMap(jobDataMap);
        }

        return job;
    }

    public MyJobBuilder withIdentity(String name) {
        key = new JobKey(name, null);
        return this;
    }

    public MyJobBuilder withIdentity(String name, String group) {
        key = new JobKey(name, group);
        return this;
    }


    public MyJobBuilder withIdentity(JobKey jobKey) {
        this.key = jobKey;
        return this;
    }

    public MyJobBuilder withDescription(String jobDescription) {
        this.description = jobDescription;
        return this;
    }

    public MyJobBuilder ofType(Class <? extends Job> jobClazz) {
        this.jobClass = jobClazz;
        return this;
    }

    public MyJobBuilder requestRecovery() {
        this.shouldRecover = true;
        return this;
    }

    public MyJobBuilder requestRecovery(boolean jobShouldRecover) {
        this.shouldRecover = jobShouldRecover;
        return this;
    }

    public MyJobBuilder storeDurably() {
        return storeDurably(true);
    }

    public MyJobBuilder storeDurably(boolean jobDurability) {
        this.durability = jobDurability;
        return this;
    }

    public MyJobBuilder usingJobData(String dataKey, String value) {
        jobDataMap.put(dataKey, value);
        return this;
    }

    public MyJobBuilder usingJobData(String dataKey, Integer value) {
        jobDataMap.put(dataKey, value);
        return this;
    }

    public MyJobBuilder usingJobData(String dataKey, Long value) {
        jobDataMap.put(dataKey, value);
        return this;
    }

    public MyJobBuilder usingJobData(String dataKey, Float value) {
        jobDataMap.put(dataKey, value);
        return this;
    }

    public MyJobBuilder usingJobData(String dataKey, Double value) {
        jobDataMap.put(dataKey, value);
        return this;
    }

    public MyJobBuilder usingJobData(String dataKey, Boolean value) {
        jobDataMap.put(dataKey, value);
        return this;
    }

    public MyJobBuilder usingJobData(JobDataMap newJobDataMap) {
        jobDataMap.putAll(newJobDataMap);
        return this;
    }

    public MyJobBuilder setJobData(JobDataMap newJobDataMap) {
        jobDataMap = newJobDataMap;
        return this;
    }

}
